package com.linefly.online_car;

import android.app.AlertDialog;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.squareup.timessquare.CalendarPickerView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

import Been.QifeiriqiListbeen;
import Util.TimeUtil;


//选择日期：根据服务器返回的时间戳，设置某些时间为高亮且可点击，此外的其他时间均不可点击
public class ChooseriqiActivity extends AppCompatActivity {
    private CalendarPickerView calendar;
    private AlertDialog theDialog;
    private CalendarPickerView dialogView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chooseriqi);
        final Calendar nextYear = Calendar.getInstance();
        nextYear.add(Calendar.YEAR, 1);
        final Calendar lastYear = Calendar.getInstance();
        lastYear.add(Calendar.YEAR, -1);
        calendar = (CalendarPickerView) findViewById(R.id.calendar_view);
        calendar.init(lastYear.getTime(), nextYear.getTime()) //getTime（）返回时间戳的函数
                .withHighlightedDates(addDateToCollection());
        calendar.scrollToDate(new Date()); //让日历滚动到当前天
        calendar.setOnDateSelectedListener(new CalendarPickerView.OnDateSelectedListener() {
            @Override
            public void onDateSelected(Date date) {
                Intent intent = new Intent();
                intent.putExtra("chooseriqi", String.valueOf(calendar.getSelectedDate().getTime()));
                Log.i("chooseriqi", "onDateSelected: " + calendar.getSelectedDate().getTime());
                //设置返回数据
                setResult(RESULT_OK, intent);
                finish();
            }

            @Override
            public void onDateUnselected(Date date) {

            }
        });

    }

    private Collection<Date> addDateToCollection() {//该函数：可显示多个时间
        QifeiriqiListbeen qifeiriqiListbeen = (QifeiriqiListbeen) getIntent().getSerializableExtra("times");
        List<String> times = new ArrayList<>();
        times = qifeiriqiListbeen.times;
        Collection<Date> date = new HashSet<Date>();
        for (int i =0;i<times.size();i++){
            long shijian = Long.valueOf(times.get(i));
            String year = TimeUtil.millis2String(shijian * 1000,new SimpleDateFormat("yyyy"));
            String month = TimeUtil.millis2String(shijian * 1000,new SimpleDateFormat("MM"));
            String day = TimeUtil.millis2String(shijian * 1000,new SimpleDateFormat("dd"));
            Log.i("shijian", "addDateToCollection: " + shijian);

            date.add(new Date(Integer.valueOf(year) - 1900,Integer.valueOf(month)-1,Integer.valueOf(day)));
        }
        return date;
    }

    @Override public void onConfigurationChanged(Configuration newConfig) {
        boolean applyFixes = theDialog != null && theDialog.isShowing();
        if (applyFixes) {
            dialogView.unfixDialogDimens();
        }
        super.onConfigurationChanged(newConfig);
        if (applyFixes) {
            dialogView.post(new Runnable() {
                @Override public void run() {
                    dialogView.fixDialogDimens();
                }
            });
        }
    }
    public void back(View view){
        finish();
    }
}
